package com.emenu;

import java.util.ArrayList;
import java.util.List;
import com.emenu.application.StaticObject;
import com.emenu.bean.Menu;
import com.emenu.bean.StackBean;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.app.ListFragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;

public class RightTopFragment extends ListFragment {

	List<Menu> list = new ArrayList<Menu>();
	Menu menu = null;
	Menu addMenu = new Menu();
	FragmentManager fm;
	StaticObject SO;
	Context cn;

	LeftFragment lf;

	public List<Menu> getList() {
		return list;
	}

	public void setList(List<Menu> list) {
		this.list = list;
	}

	private MemberListener memberListener;

	public void setMemberListener(MemberListener memberListener) {
		this.memberListener = memberListener;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getDate();
		setListAdapter(new RightAdapter());
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		SO = (StaticObject) getActivity().getApplication();
		cn = this.getActivity();
		fm = ((FragmentMAin) cn).getSupportFragmentManager();

		lf = (LeftFragment) fm.findFragmentById(R.id.left);
		list = SO.getMenuList();
		
		Menu addMenu3 = new Menu();
		addMenu3.setCode("quanbu");
		addMenu3.setName("全部");
		addMenu3.setC1(-1);
		list.add(addMenu3);
		
		Menu addMenu = new Menu();
		addMenu.setCode("lengcai");
		addMenu.setName("热菜");
		addMenu.setC1(0);
		list.add(addMenu);
		Menu addMenu1 = new Menu();
		addMenu1.setCode("recai");
		addMenu1.setName("冷菜");
		addMenu1.setC1(1);
		list.add(addMenu1);
		Menu addMenu2 = new Menu();
		addMenu2.setCode("jiushui");
		addMenu2.setName("酒水");
		addMenu2.setC1(2);
		list.add(addMenu2);

	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		l.setItemChecked(position, true);
int cc=list.get(position).getC1();
		if (memberListener != null) {
			if (cc >= -1) {
				FragmentTransaction ft = fm.beginTransaction();

				FragmentHaveHit HaveHit = new FragmentHaveHit();
				Bundle args = new Bundle();
				args.putInt("c1", cc);
				HaveHit.setArguments(args);
				ft.replace(R.id.left, HaveHit);
				ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
				ft.addToBackStack(null);

				ft.commit();
				if(cc==-1){
				((FragmentMAin) cn).page_pathvalue.setText("全部");
				}else if(cc==0){
					((FragmentMAin) cn).page_pathvalue.setText("热菜");
				}else if(cc==1){
					((FragmentMAin) cn).page_pathvalue.setText("冷菜");
				}else if(cc==2){
					((FragmentMAin) cn).page_pathvalue.setText("酒水");
				}
				((FragmentMAin) cn).h++;
			}else{
			
			memberListener.onMemberSelected(list.get(position));

			StackBean sb = new StackBean();
			sb.setName(list.get(position).getName());
			SO.getStackList().clear();
			SO.getStackList().add(sb);

			for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
				fm.popBackStack();
			}
			((FragmentMAin) cn).showpath();
			}

			
		}
	}

	private List<Menu> getDate() {

		return list;
	}

	public class RightAdapter extends BaseAdapter {

		@Override
		public int getCount() {

			return list.size();
		}

		@Override
		public Object getItem(int position) {

			return list.get(position);
		}

		@Override
		public long getItemId(int position) {

			return position;

		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = ((FragmentMAin) cn).getLayoutInflater().inflate(
						R.layout.right, null);
			}
			TextView textViewname;
			TextView textViewcount;

			textViewname = (TextView) convertView
					.findViewById(R.id.tv_rightname);
//			if (position % 2 == 1) {
//				textViewname.setBackgroundColor(R.color.right2);
//
//			} else {
//				textViewname.setBackgroundColor(R.drawable.menutop1);
//			}
			
			if (position % 2 == 0) {
				textViewname.setBackgroundColor(R.color.right2);
//				textViewname.setBackgroundColor(R.drawable.menutop1);

//			} else if (position % 2 == 0){
//				textViewname.setBackgroundColor(R.color.rightbg);
			}

			textViewname.setText((String) list.get(position).getName());

			textViewcount = (TextView) convertView.findViewById(R.id.tv_count);
			textViewcount
					.setText(String.valueOf(list.get(position).getCount()));

//			convertView
//					.setBackgroundColor((position % 2) == 1 ? R.drawable.menutop1
//							: R.drawable.menutop2);
			// if(list.get(position).getC1()>=0){
			// convertView.setBackgroundColor(Color.BLACK);
			// }

			return convertView;

		}

	}

}
